package com.powerwyx.shortlink.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powerwyx.shortlink.admin.common.biz.user.UserContext;
import com.powerwyx.shortlink.admin.common.convention.exception.ServiceException;
import com.powerwyx.shortlink.admin.common.convention.result.Result;
import com.powerwyx.shortlink.admin.pojo.entity.Group;
import com.powerwyx.shortlink.admin.remote.ShortLinkActualRemoteService;
import com.powerwyx.shortlink.admin.remote.dto.ShortLinkRecycleBinPageDTO;
import com.powerwyx.shortlink.admin.remote.vo.ShortLinkPageVO;
import com.powerwyx.shortlink.admin.service.GroupService;
import com.powerwyx.shortlink.admin.service.RecycleBinService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 王艺锡
 * @version 1.0
 */
@Service
@Slf4j
public class RecycleBinServiceImpl implements RecycleBinService {

    @Autowired
    private GroupService groupService;

    @Autowired
    private ShortLinkActualRemoteService shortLinkActualRemoteService;


    /**
     * 分页查询回收站中的短链接
     *
     * @param shortLinkRecycleBinPageDTO
     * @return
     */
    @Override
    public Result<Page<ShortLinkPageVO>> shortLinkRecycleBinPage(ShortLinkRecycleBinPageDTO shortLinkRecycleBinPageDTO) {

        List<Group> groupList = groupService.list(new QueryWrapper<Group>()
                .eq("username", UserContext.getUsername())
                .eq("del_flag", 0));

        if(CollUtil.isEmpty(groupList)){
            throw new ServiceException("用户无分组信息");
        }
        shortLinkRecycleBinPageDTO.setGidList(groupList.stream().map(Group::getGid).collect(Collectors.toList()));

        return shortLinkActualRemoteService.shortLinkRecycleBinPage(
                shortLinkRecycleBinPageDTO.getGidList(),
                shortLinkRecycleBinPageDTO.getCurrent(),
                shortLinkRecycleBinPageDTO.getSize()
                );
    }
}
